package com.tangding.hprose.api.server.command.impl.watch;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tangding.cache.GamesDataWatchingCache;
import com.tangding.common.exception.GException;
import com.tangding.common.logx.LogHelp;
import com.tangding.hprose.api.client.HproseAsClient;
import com.tangding.hprose.api.server.command.Command;
import com.tangding.hprose.api.server.command.Response;
import com.tangding.net.pool.BaseNettySessionPool;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年12月24日_下午3:15:12
 *
 */
@Component
public class WatchingGamesData implements Command{

	@Autowired
	BaseNettySessionPool pool;
	
	@Override
	public Response exe(Object... params) throws GException {
		@SuppressWarnings("unchecked")
		Map<String, Object> user = (Map<String, Object>) params[params.length - 1];
		String uid = (String) user.get("uid");
		GamesDataWatchingCache.addWatching(uid);
		
		poolExe.submit(new Runnable() {
			public void run() {
				try {
					Object result = HproseAsClient.invoke("QueryGamesData");
					pool.doResponse(uid, "UpdateGamesData", result);
				} catch (GException e) {
					LogHelp.doLogAppErr(e);
				}
			}
		});
		
		Object[] newParams = new Object[params.length - 1];
		System.arraycopy(params, 0, newParams, 0, newParams.length);
		Object result = HproseAsClient.invoke("QueryGamesSettings", newParams);
//		return doResponseSucc(JacksonUtil.readValue(result.toString(), Map.class));
		return doResponseSucc(result);
	}

}
